/*
*******************************************************************************
* ansi c source code
* file name:
*	ReadFile.c
* abstract:
*	to read data from a specified file
* author:Fangming He
*******************************************************************************
*/

/*
*******************************************************************************
*                               include files
*******************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "globaldef.h"

/*
*******************************************************************************
*                            function definition
*******************************************************************************
*/
/*
******************************************************************************* 
* description:
*	find a character in the file
* input: 
*	pf: pointer to the file
*	target: the character to be searched
* output:
*	if find, return 1; otherwise retrun 0.
* function reference : 
* author :
*	guo liang 2004-5-28	created
*******************************************************************************
*/


int FindChar(FILE *pf, char target)
{
	char ch;

	do
	{
		ch = fgetc(pf);
	} while( (ch != target) && (!feof(pf)) );

	if ( ch == target ) return 1;
	else return 0;
}

/*
******************************************************************************* 
* description: find a string in the file
* inputs: 
*	pf: pointer to the file
*	p_str: pointer to the string to be searched
* output:
*	if find, return 1; otherwise retrun 0.
* function reference: 

*******************************************************************************
*/
int FindString(FILE *pf, char *p_str)
{
	unsigned int	length;	/* length of the string */
	unsigned int	i;
	long			fpos;	/* position in the file */
	char			find;	/* indicator */

	length = strlen(p_str);
	if ( length == 0 ) return 0;

	if ( !FindChar(pf, p_str[0]) ) return 0;
	fpos = ftell(pf);
	find = 0;

	while ( (!find) && (!feof(pf)) )
	{
		find = 1;
		for (i = 1; i < length; i++)
		{
			if ( fgetc(pf) != p_str[i] )
			{
				find = 0;
				break;
			}
		}

		if ( find ) break;

		fseek(pf, fpos+1, 0);
		if ( !FindChar(pf, p_str[0]) ) return 0;
		fpos = ftell(pf);
	}

	return 1;
}
/*
******************************************************************************* 
* description:
*	change the current position to a new line
* input: 
*	pf: pointer to the file
* output:
*	if succeed, return 1; otherwise retrun 0.
* function reference: 

*******************************************************************************
*/
int ChangeLine(FILE *pf)
{
	if ( !FindChar(pf, 0x0a) ) return 0;

	return 1;
}
/*
******************************************************************************* 

/*
******************************************************************************* 
* description:
*	read parameters from a specified file. The input file must be such a 
*	pattern below:
*	-- xxxxxxx(xx could be annotations)
*	parameter1
*	-- xxxxxxx
*	parameter2
*	......
* input: 
*	file_name: name of file
* output:
*	*SNR: the first parameter1 from the file
*   *M_Type: the second parameter1 from the file
*	*FFT_Size: the third parameter1 from the file
*   *CP_Length: the forth parameter1 from the file
* function reference: 
* author:
*	guo liang, 2006-5-30	created
*******************************************************************************
*/
int ReadFile(char file_name[], int *M_Type, double *SNR,int *FFT_Size , int *CP_Length)
{
	FILE *pf;
	double tmp[5];
	int i;
	pf = fopen(file_name, "r");
	i=0;
	if ( pf == NULL )
	{
		return 0;
	}
	while(FindString(pf, "--"))
	{
		if ( !ChangeLine(pf) ) break;
		fscanf(pf,"%lf",&tmp[i]);
		i++;
	}
	*SNR = tmp[0];
	*M_Type = (int)(tmp[1]);
	*FFT_Size = (int)tmp[2];
	*CP_Length = (int)tmp[3];
	fclose(pf);
	return 1;
}
/*
******************************************************************************* 
* description:
*	read a matrix from a specified file. The matrix file should be saved as 
*   a row should consists of PILOT_NUM element
* input: 
*	file_name: name of file
* output:
*	Matrix[][PILOT_NUM] : the Matrix read form the file
* function reference: 
* author:
*	guo liang, 2006-5-30	created
*******************************************************************************
*/

int ReadMatrix(char file_name[],struct Complex Matrix[][PILOT_NUM])
{
	static char ch_real[20];
	static char ch_imag[20];
	static char ch_complex[40];
	double tmp;
	char ch;
	int  flag1;    /* flag for real or imag */
	int  flag[2];    /* flag for '+' or '-' for real and imag */ 
	int  count_Minus;
	int  count_Plus;
	int  k;
	int  i;
	int  m;
	int  j;
	int  round;
	int count_c;  /* count the column number which has benn read */
	int count_r;  /* count the row number which has benn read */
	FILE *pf;
	pf = fopen(file_name, "r");
	if ( pf == NULL )
	{
		printf("Read file error");
		return 0;
	}
	count_r = 0;
	count_c = 0;
	count_Minus = 0;
	count_Plus  = 0;
	flag1 = 0;
	k = 0;
    for ( count_r = 0 ; count_r < BLOCK_SIZE ; count_r++ )
	{
		
		while ( count_c < PILOT_NUM )
		{
			ch = fgetc(pf);
			while ( ch != 'i')
			{
				ch_complex[k++] = ch;
				if (ch == '-')
				{
					count_Minus++;
					ch = fgetc(pf);
					continue;
				}
				if (ch == '+')
				{
					count_Plus++;
					ch = fgetc(pf);
					continue;
				}
				ch = fgetc(pf);
			}
			i = 0;
			j = 0;
			m = 0;
			for ( round = 0; round < k; round++)
			{
				if (count_Minus == 2)
				{
					flag[0] = -1;
					flag[1] = -1;
					if (ch_complex[round] == '-')
					{
						m++;
					}
					switch(m)
					{
					case 1: 
						{
							flag1 = 0;
							break;
						}
					case 2:
						{
							flag1 = 1;
							break;
						}
					default :
						break;
					}
					if ( flag1 == 0)
					{
						if ((ch_complex[round] == '.') || ((ch_complex[round]>47) &&(ch_complex[round]<58) ))
						{
							ch_real[i++] = ch_complex[round];
						}
					}
					if ( flag1 == 1)
					{
						if ((ch_complex[round] == '.') || ((ch_complex[round]>47) &&(ch_complex[round]<58) ))
						{
							ch_imag[j++] = ch_complex[round];
						}
					}
				}
				else 
				{
					if ( count_Plus == 1)
					{
						if (ch_complex[round] == '-')
						{
							flag[0] = -1;
						}
						if (ch_complex[round] == '+')
						{
							flag[1] = 1;
							flag1 = 1;
						}
						
					
					}
					else
					{
						if (ch_complex[round] == '-')
						{
							flag[0] = 1;
							flag[1] = -1;
							flag1 = 1;
						}
					}
					if ( flag1 == 0 )
					{
						if ((ch_complex[round] == '.') || ((ch_complex[round]>47) &&(ch_complex[round]<58) ))
						{
							ch_real[i++] = ch_complex[round];
						}
					}
					if  (flag1 == 1) 
					{
						if ((ch_complex[round] == '.')||((ch_complex[round]>47) &&(ch_complex[round]<58) ))
						{
							ch_imag[j++] = ch_complex[round];
						}
					}
				}
			}
			tmp = 0;
			for ( k = 0; k < 14 ; k++)
			{
				tmp += ((ch_real[k+2]-48)/pow(10,k+1));
			}
			Matrix[count_r][count_c].real = tmp * flag[0];
			tmp = 0;
			for ( k = 0; k < 14 ; k++)
			{
				tmp += ((ch_imag[k+2]-48)/pow(10,k+1));
			}
			Matrix[count_r][count_c].imag = tmp * flag[1];
			flag1 = 0;
			flag[0] = 1;
			flag[1] = 1;
			j = 0;
			i = 0;
			k = 0;
			count_Minus = 0;
			count_Plus = 0;
			count_c++;
		}
		count_c = 0;
		if ( !ChangeLine(pf) ) break;
	}
	fclose(pf);
	return 1;
}
/*
******************************************************************************* 
* description:
*	read data from a specified file. 
* input: 
*	file_name: name of file
*	*input : the address  of the data read from the file
*   *length : the data length been read
* output:
* function reference: 
* author:
*	guo liang, 2006-5-30	created
*******************************************************************************
*/

int ReadData (char file_name[], int *input ,int *length)
{
	FILE *pf;
	char ch;
	int count;
	pf = fopen(file_name, "r");
	if ( pf == NULL )
	{
		return 0;
	}
	count = 0;
	while (!feof(pf))
	{
		ch = fgetc (pf);
		if ( (ch == 49) || (ch == 48) )
		{
			input[count] = 2 * (int) (ch-48) - 1;
			count++;			
		}
	}
	*length = count;
	fclose(pf);
	return 1;
}


